package cn.pzaily.fealetter.server.impl;

import cn.pzaily.fealetter.entity.ConfigEntity;
import cn.pzaily.fealetter.enumeration.RedisPropertiesKey;
import cn.pzaily.fealetter.mapper.ConfigMapper;
import cn.pzaily.fealetter.model.ServerTopic;
import cn.pzaily.fealetter.server.ConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @PackageName: cn.pzaily.fealetter.server
 * @Description:
 * @Author: Pang
 * @Date: 2021-11-20 20:07
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, ConfigEntity> implements ConfigService {


    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void sendUpdateMessage() {
        Map serverMap = redisTemplate.opsForHash().entries(RedisPropertiesKey.SERVER_ID_HASH);
        long currentTimeMillis = System.currentTimeMillis();
        for (Object key : serverMap.keySet()) {
            Object value = serverMap.get(key);
            long parseLong = Long.parseLong(value.toString());
            if ((currentTimeMillis - parseLong) < (1000 * 60 * 5)) {
                redisTemplate.delete("listForConnect");
                ServerTopic serverTopic = ServerTopic.builder().option(ServerTopic.INIT_CONNECT).value(key.toString()).build();
                redisTemplate.convertAndSend("TOPIC_HIGH_SERVER", serverTopic.toString());
                return;
            }
        }

    }
}
